package com.xr.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xr.common.constant.MiniProgramConstants;
import com.xr.miniprogram.domain.MiniProgramUserAddress;
import com.xr.miniprogram.mapper.MiniProgramUserAddressMapper;
import com.xr.miniprogram.service.IMiniProgramUserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class IMiniProgramUserAddressServiceImpl extends ServiceImpl<MiniProgramUserAddressMapper, MiniProgramUserAddress> implements IMiniProgramUserAddressService {

    @Override
    public List<MiniProgramUserAddress> queryUserAddressList(Long miniProgramUserId) {
        return this.list(new QueryWrapper<MiniProgramUserAddress>().lambda().eq(MiniProgramUserAddress::getMiniProgramUserId, miniProgramUserId));
    }

    @Override
    public MiniProgramUserAddress queryUserAddressById(Long miniProgramUserAddressId) {
        return this.getById(miniProgramUserAddressId);
    }

    @Override
    public MiniProgramUserAddress defaultAddress(Long userId) {
        QueryWrapper<MiniProgramUserAddress> miniProgramUserAddressQueryWrapper = new QueryWrapper<>();
        miniProgramUserAddressQueryWrapper.lambda().eq(MiniProgramUserAddress::getMiniProgramUserId,userId).eq(MiniProgramUserAddress::getDefaultStatus, MiniProgramConstants.ADDRESS_DEFAULT);
        return this.getOne(miniProgramUserAddressQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateAddress(MiniProgramUserAddress miniProgramUserAddress) {
        //新增或更新地址使，对默认地址进行判断处理
        //更新：改变默认状态为否（不处理）  改变默认状态为是（处理）
        //新增：不为默认（不处理）  设置为默认（处理）

        if(miniProgramUserAddress.getDefaultStatus().equals(MiniProgramConstants.ADDRESS_DEFAULT)){
            //设置为默认地址 改变原有默认地址
            this.setDefaultAddressToNot(miniProgramUserAddress.getMiniProgramUserId());
        }
        return this.saveOrUpdate(miniProgramUserAddress);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeDefault(MiniProgramUserAddress miniProgramUserAddress) {
        //传递用户id 与 设置为默认的地址id
        //查询当前默认的地址id
        //如果当前id == 传递的地址id 取消默认地址，设置为0
        //如果当前id ！= 传递的地址id 设置当前的为0，新的为1
        if (miniProgramUserAddress.getMiniProgramUserId()==null || miniProgramUserAddress.getMiniProgramUserId()==0){
            return false;
        }else{
            Long defaultAddressId = this.setDefaultAddressToNot(miniProgramUserAddress.getMiniProgramUserId());

            //判断传递的id是否与当前相同
            if(defaultAddressId.equals(miniProgramUserAddress.getMiniProgramUserAddressId())){
                return true;
            }else{
                //设置新的默认地址
                miniProgramUserAddress.setDefaultStatus(MiniProgramConstants.ADDRESS_DEFAULT);
                return this.updateById(miniProgramUserAddress);
            }
        }
    }

    /**
     * 根据userId将目前的默认地址改为否
     * @param miniProgramUserId
     * @return
     */
    public Long setDefaultAddressToNot(Long miniProgramUserId){
        LambdaQueryWrapper<MiniProgramUserAddress> miniProgramUserAddressQueryWrapper = new QueryWrapper<MiniProgramUserAddress>().lambda();
        //查询当前默认的地址
        miniProgramUserAddressQueryWrapper.eq(MiniProgramUserAddress::getMiniProgramUserId,miniProgramUserId);
        miniProgramUserAddressQueryWrapper.eq(MiniProgramUserAddress::getDefaultStatus,MiniProgramConstants.ADDRESS_DEFAULT);
        MiniProgramUserAddress defaultAddress = this.getOne(miniProgramUserAddressQueryWrapper);
        if(defaultAddress == null) return 0L;
        //将当前的设置为0
        defaultAddress.setDefaultStatus(MiniProgramConstants.ADDRESS_NOT_DEFAULT);
        this.updateById(defaultAddress);
        return defaultAddress.getMiniProgramUserAddressId();
    }
}
